package com.example.code;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import com.baomidou.mybatisplus.generator.fill.Column;
import com.baomidou.mybatisplus.generator.function.ConverterFileName;
import com.example.path.PathUtil;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class MyBatisPlusGenerator {

    public static void main(String[] args) {
        String url = "jdbc:mysql://localhost:3306/test?serverTimezone=UTC";
        String username = "root";
        String password = "123";



        String Author = "ZhengDing";
        String ProjectDir = PathUtil.getCanonicalPath();
        String outputDir = ProjectDir + "\\src\\main\\java";
        String ParentPackage = "com.example";
        String ModuleName = "sys";
        List<String> IncludeTables = Arrays.asList("xf_user", "xf_article");
        String mapperXmlDir = ProjectDir + "\\src\\main\\resources\\mapper";


        FastAutoGenerator.create(url, username, password)
                .globalConfig(builder -> {
                    builder.author(Author) // 设置作者
                            .commentDate("yyyy-MM-dd HH:mm:ss")
                            .enableSwagger() // 开启 swagger 模式
                            .fileOverride() // 覆盖已生成文件
                            .outputDir(outputDir); // 指定输出目录
                })
                .packageConfig(builder -> {
                    builder.parent(ParentPackage) // 设置父包名
                            .moduleName(ModuleName) // 设置父包模块名
                            .pathInfo(Collections.singletonMap(OutputFile.mapperXml, mapperXmlDir)); // 设置mapperXml生成路径
                })
                .strategyConfig(builder -> {
                    builder.addInclude(IncludeTables).addTablePrefix("t_", "c_", "xf_")

                            .controllerBuilder().enableRestStyle().enableHyphenStyle()
                            .serviceBuilder().formatServiceFileName("%sService")

                            .entityBuilder()
                            .enableLombok()
                            .enableChainModel()
                             .enableColumnConstant()
                            .enableTableFieldAnnotation()
                            .idType(IdType.AUTO)
                            // .enableActiveRecord()
                            .versionColumnName("version")
                            .versionPropertyName("version")
                            .logicDeleteColumnName("deleted")
                            .logicDeletePropertyName("deleteFlag")
                            // .superClass("MyBaseEntity")
                            // .addSuperEntityColumns("id", "created_by", "created_time", "updated_by", "updated_time")
                            .addIgnoreColumns("age")
                            .addTableFills(new Column("create_time", FieldFill.INSERT))
                            .addTableFills(new Column("update_time", FieldFill.INSERT_UPDATE));
                })
                .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板，默认的是Velocity引擎模板
                .execute();
    }
}

